package com.abc.cloud.config;

import com.abc.cloud.common.filter.PathInterceptor;
import com.abc.cloud.common.filter.TokenInterceptor;
import com.abc.cloud.service.MenuService;
import com.abc.cloud.service.PermsService;
import com.abc.cloud.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Autowired
    private UserService userService;

    @Autowired
    private PermsService permsService;

    @Value("secure.ignored.urls")
    private List<String> urls;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new TokenInterceptor(userService, permsService))
                .addPathPatterns("/**")
                .excludePathPatterns("/user/userRegister", "/user/userLogin", "/captcha/**",
                        "/user/adminLogin", "/file/getFile/**", "/fileShare/info/**", "/fileShare/getFile",
                        "/fileShare/download/**")
                .excludePathPatterns("/webjars/**", "/error", "/swagger-resources/**", "/doc.html",
                        "/v2/**", "/swagger-ui/**", "/favicon.ico")
                .order(1);

        registry.addInterceptor(new PathInterceptor(urls, permsService))
                .addPathPatterns("/**").order(2);

    }

}
